// pages/report/index.js
import ajax from '../network/index'
let _this;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    index: "", //上一个页面传过来的数据
    options: [],
    activeName: [1, 10],
    activeNames: "10",
    currentCode: "", //传到组件内的值
    currentName: "",
  },
  onChange(event) {
    this.setData({
      activeName: event.detail,
    });
  },
  _confirm() {
    if (!this.data.currentCode) {
      wx.showToast({
        title: '当前未选择科目',
        icon: "none"
      })
      return
    }
    let pages = getCurrentPages()
    let prevPage = pages[pages.length - 2]
    let list = prevPage.data.list
    let index = this.data.index
    prevPage.setData({
      [`list[${index}].subjectCode`]: this.data.currentCode,
      [`list[${index}].subjectName`]: this.data.currentName,
    })
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  setCurrentCode(e) {
    console.log(e.detail);
    this.setData({
      currentCode: e.detail.subjectCode,
      currentName: e.detail.subjectName,
    })
  },
  onLoad: function (options) {
    console.log(options, 'options');
    _this = this
    this.setData({
      index: options.index,
      currentCode: options.code,
      currentName: options.name
    })
    let list = wx.getStorageSync('reportList')
    if (list && list.length) {
      this.setData({
        options: list
      })
    } else {
      this.getSubjectList();
    }
  },
  async getSubjectList() {
    wx.showLoading({
      title: '数据加载中...',
    })
    let res = await _this.subjectListFnc();
    if (res.code == 200 && res.data && res.data.content) {
      _this.setData({
        list: res.data.content
      })
      _this.getAllList(_this.data.list);
    }
  },

  //获取一级科目列表
  async subjectListFnc(data) {
    return ajax({
      url: "/subject/oneLevelSubjectList",
      method: "POST",
      data
    })
  },
  //获取科目的所有子级
  async allSubjectListFnc(subjectCode) {
    return ajax({
      url: "/subject/subjectNextAllSubjectList",
      method: "POST",
      data: {
        subjectCode
      }
    })
  },
  // 递归得到所有的科目选项
  async getAllList(list) {
    let i = 0;
    if (list && list.length > 0) {
      list.forEach((item) => {
        _this.allSubjectListFnc(item.subjectCode).then((res) => {
          i++;
          item["list"] = res.data.content;
          if (i == list.length) {
            wx.hideLoading()
            _this.setData({
              options: _this.setArr(list)
            })
            wx.setStorageSync('reportList', _this.data.options)
          }
        });
      });
    }
    return list;
  },
  //设置子级没有值的时候就删除
  setArr(list) {
    if (list && list.length > 0) {
      list.forEach((item) => {
        if (item.list && item.list.length > 0) {
          this.setArr(item.list);
        } else {
          delete item.list;
        }
      });
    }
    return list
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})